package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //分页——条件查询
    @GetMapping
    public Result list(ClazzQueryParam  param) {
        log.info("查询班级列表");
        PageResult<Clazz> pageResult = clazzService.getPageResult(param);
        return Result.success(pageResult);
    }

    //新增班级
    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);

        //调用service层方法保存班级信息及班主任的姓名
        clazzService.saveClazz(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);
        clazz.setUpdateTime(LocalDateTime.now());
        clazzService.updateById(clazz);
        return Result.success();
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);
        clazzService.deleteById(id);
        return Result.success();
    }

    //下拉框展示
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");
        List<Clazz> clazzs = clazzService.list();
        return Result.success(clazzs);
    }
}
